local jiumeng_longquan = fk.CreateSkill {

  name = "jiumeng_longquan",

  tags = { Skill.Compulsory, },

}



jiumeng_longquan:addEffect("viewas", {
  name = "jiumeng_longquan",
  anim_type = "offensive",
  pattern = "slash",
  prompt  = "jiumeng_longquan_fireslash",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("fire__slash")
    card.skillName = "jiumeng_longquan"
    card:addSubcard(cards[1])
    return card
  end,
  before_use = function (self, player, use)
    use.additionalDamage = (use.additionalDamage or 0) + 1
    use.disresponsiveList = player.room.alive_players
  end,
  after_use = function (self, player, use)
    if player:usedSkillTimes(jiumeng_longquan.name,Player.HistoryGame) == 1 then
      player:drawCards(3,jiumeng_longquan.name)
    end
    player.room:setPlayerMark(player,"jiumeng_longquan_trslash-turn",1)
  end,
  enabled_at_play = function (self, player)
    return player:getMark("jiumeng_longquan_trslash-turn") == 0
  end,
  enabled_at_response = Util.FalseFunc,
})

jiumeng_longquan:addEffect(fk.TargetSpecified, {
  name = "#jiumeng_longquan_trigger",
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("jiumeng_longquan") then  
        if target == player and data.card and data.card.trueName == "slash"  then
              return true
          end
      end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
      data.to:addQinggangTag(data)
  end,
})
jiumeng_longquan:addEffect(fk.CardUsing, {
  name = "#jiumeng_longquan_trigger",
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("jiumeng_longquan") then
        if target == player and data.card and data.card.trueName == "slash"  then
              return not table.contains(player:getTableMark("jiumeng_longquan_hasevoked"),data.card.name) 
        end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      return room:askToSkillInvoke(player,{ skill_name = jiumeng_longquan.name, prompt = "#jiumeng_longquan_draw"})
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
      room:addTableMarkIfNeed(player,"jiumeng_longquan_hasevoked",data.card.name)
      if data.card.name == "slash" then
        player:drawCards(2,jiumeng_longquan.name)
      else
        player:drawCards(3,jiumeng_longquan.name)  
      end
  end,
})

jiumeng_longquan:addEffect(fk.PreCardEffect, {
  name = "#jiumeng_longquan_trigger",
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("jiumeng_longquan") then
         if target ~= player and data.responseToEvent and data.card.name == "jink" then
            local src = data.responseToEvent.from
            return src == player
         end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
      return not target:isKongcheng() and room:askToSkillInvoke(player,{ skill_name = jiumeng_longquan.name, prompt = "#jiumeng_longquan_discard_evoke::"..target.id})
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
      local card = room:askToChooseCard(player,{target = target, flag = "h", skill_name = jiumeng_longquan.name,  prompt = "#jiumeng_longquan_discard::"..target.id})
      room:throwCard(card,jiumeng_longquan.name,target,player)
  end,
})

jiumeng_longquan:addEffect(fk.DamageCaused, {
  name = "#jiumeng_longquan_trigger",
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("jiumeng_longquan") then
        if target == player and data.card and data.card.trueName == "slash"  then
            local isTransformed = data.card:isVirtual() or (data.card.skillName and data.card.skillName ~= "")
            if isTransformed and #data.card.subcards > 0 then
              local canevoke = true
              for _, v in ipairs(data.card.subcards) do
                if Fk:getCardById(v).type ~= Card.TypeEquip then
                  canevoke = false
                end
              end
              return canevoke
            end
        end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
      room:loseHp(data.to,data.damage,jiumeng_longquan.name)
      data:preventDamage()
  end,
})

jiumeng_longquan:addEffect(fk.TurnStart, {
  name = "#jiumeng_longquan_trigger",
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("jiumeng_longquan") then
        return true
    end
  end,
  on_cost = function (self, event, target, player, data)
      player.room:addPlayerMark(player,MarkEnum.SlashResidue.."-turn",2)
      player.room:addPlayerMark(player,MarkEnum.SlashBypassDistances.."-turn",3)
  end,
})

return jiumeng_longquan